package com.xfsong.demo.controller;

import com.xfsong.demo.common.R;
import com.xfsong.demo.entity.Category;
import com.xfsong.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 插入商品分类
    @PostMapping
    public R insertCategory(@RequestBody Category category) {
        if (category.getSort() == null) {
            return R.fail("排序字段不能为空");
        }

        if (categoryService.isCategoryNameExists(category.getCategoryName())) {
            return R.fail("商品分类名已存在");
        }
        int result = categoryService.insertCategory(category);
        if (result > 0) {
            return R.ok("商品分类添加成功");
        } else {
            return R.fail("商品分类添加失败");
        }
    }

    // 根据 ID 删除商品分类
    @DeleteMapping("/{id}")
    public R deleteCategoryById(@PathVariable Integer id) {
        if (id == null) {
            return R.fail("分类 ID 不能为空");
        }
        if (!categoryService.canDeleteCategory(id)) {
            return R.fail("该分类下存在商品，不能删除");
        }
        int result = categoryService.deleteCategoryById(id);
        if (result > 0) {
            return R.ok("商品分类删除成功");
        } else {
            return R.fail("商品分类删除失败");
        }
    }

    // 更新商品分类
    @PutMapping
    public R updateCategory(@RequestBody Category category) {
        if (category.getId() == null) {
            return R.fail("分类 ID 不能为空");
        }
        if (category.getSort() == null) {
            return R.fail("排序字段不能为空");
        }

        // 检查当前分类名是否和其他分类重复（排除当前分类自身）
        Category existingCategory = categoryService.selectCategoryByName(category.getCategoryName());
        if (existingCategory != null && !existingCategory.getId().equals(category.getId())) {
            return R.fail("商品分类名已存在");
        }
        int result = categoryService.updateCategory(category);
        if (result > 0) {
            return R.ok("商品分类更新成功");
        } else {
            return R.fail("商品分类更新失败");
        }
    }

    private R getResponseEntity(@RequestBody Category category) {
        if (category.getCategoryName() == null || category.getCategoryName().isEmpty()) {
            return R.fail("分类名不能为空");
        }
        return null;
    }

    // 根据 ID 查询商品分类
    @GetMapping("/{id}")
    public R selectCategoryById(@PathVariable Integer id) {

        if (id == null) {
            return R.fail("分类 ID 不能为空");
        }
        return R.ok(categoryService.selectCategoryById(id));
    }

    // 查询所有商品分类
    @GetMapping
    public R selectCategoriesByPageAndName(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int pageSize,
            @RequestParam(value = "name", required = false) String categoryName) {

        List<Category> categories = categoryService.selectCategoriesByPageAndName(page, pageSize, categoryName);
        int totalCount = categoryService.countCategoriesByName(categoryName);
        Map<String, Object> result = new HashMap<>();
        result.put("categories", categories);
        result.put("totalCount", totalCount);
        return R.ok(result);
    }
}
